<?php
namespace app\shop\model;

use app\BaseModel;
use app\farm\model\User;
use longbingcore\wxcore\PushMsgModel;
use think\facade\Db;

class SigninRecord extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_signin_record';



    protected $append = [

        'create_time_text'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-25 11:37
     */
    public function getCreateTimeTextAttr($value,$data){

        if(!empty($data['create_time'])){

            return date('Y-m-d H:i',$data['create_time']);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 16:11
     * @功能说明:判断是否是连续签到
     */
    public function initSign($user_id){

        $date = date('Y-m-d',strtotime('-1 day'));

        $today = date('Y-m-d',time());

        $dis = [

            'user_id' => $user_id,

            'create_date' => $date,

            'status' => 1
        ];

        $find = $this->dataInfo($dis);

        if(empty($find)){

            $where[] = ['user_id','=',$user_id];

            $where[] = ['status','=',1];

            $where[] = ['create_date','<>',$today];

            $this->dataUpdate($where,['status'=>0]);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-26 14:55
     * @功能说明:发送消息
     */
    public function sendMsg($uniacid){

        $user_model = new User();

        $time = date('H',time());

        if($time<10){

            return false;
        }

        $push_model = new PushMsgModel($uniacid);

        $today = strtotime(date('Y-m-d',time()));
        //开启了签到提醒
        $dis[] = ['uniacid','=',$uniacid];
        //今天没有发送过消息
        $dis[] = ['sign_notice_time','<',$today];
        //今天未签到
        $dis[] = ['sign_time','<',$today];

        $data = $user_model->where($dis)->field('id,uniacid,id as user_id')->limit(10)->select()->toArray();

        if(!empty($data)){

            foreach ($data as $v){

                $res = $user_model->where(['id'=>$v['user_id']])->where('sign_notice_time','<',$today)->update(['sign_notice_time'=>time()]);

                if($res!=0){

                    $push_model->sendMsg($v,14);
                }

            }

        }

        return true;
    }





}